//
//  JackStringExtension.swift
//  JourneyThroughTime
//
//  Created by iOS-Czz on 2023/11/6.
//

import Foundation
extension String {
    func dayOfWeek() -> String? {
            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = "yyyy.MM.dd"

            if let date = dateFormatter.date(from: self) {
                dateFormatter.dateFormat = "EEEE"
                return dateFormatter.string(from: date)
            }
            return nil
        }
    static func write(toFile fileName: String, content: String) {
        let fileManager = FileManager.default
        do {
            if let documentsURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first {
                let fileURL = documentsURL.appendingPathComponent(fileName)
                try content.write(to: fileURL, atomically: true, encoding: .utf8)
                print("String written to file: \(fileURL.absoluteString)")
            }
        } catch {
            print("Error writing to file: \(error)")
        }
    }
    
    static func read(fromFile fileName: String) -> String? {
        let fileManager = FileManager.default
        do {
            if let documentsURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first {
                let fileURL = documentsURL.appendingPathComponent(fileName)
                let content = try String(contentsOf: fileURL, encoding: .utf8)
                print("String read from file: \(fileURL.absoluteString)")
                return content
            }
        } catch {
            print("Error reading from file: \(error)")
        }
        return nil
    }
    static func read(fromFilePath filePath: String) -> String? {
        do {
            let content = try String(contentsOfFile: filePath, encoding: .utf8)
            print("String read from file: \(filePath)")
            return content
        } catch {
            print("Error reading from file: \(error)")
            return nil
        }
    }
}
extension NSMutableAttributedString {
    
}
extension NSAttributedString {
    static func coloredString(originalText: String, coloredSubText: String, colorForColoredText: UIColor) -> NSAttributedString {
        let attributedString = NSMutableAttributedString(string: originalText)
        
        // 查找coloredSubText在原始文本中的范围
        if let range = originalText.range(of: coloredSubText) {
            let nsRange = NSRange(range, in: originalText)
            
            // 设置coloredSubText的颜色
            attributedString.addAttribute(.foregroundColor, value: colorForColoredText, range: nsRange)
        }
        
        return attributedString
    }
    
}
